<template>
    <div>
      <nav-header></nav-header>
      <nav-bread>
        <span>Goods</span>
      </nav-bread>
      <div class="accessory-result-page accessory-page">
        <div class="container">
          <div class="filter-nav">
            <span class="sortby">Sort by:</span>
            <a href="javascript:void(0)" class="default cur">Default</a>
            <a href="javascript:void(0)" @click="sortGoods" class="price">Price <svg class="icon icon-arrow-short"><use xlink:href="#icon-arrow-short"></use></svg></a>
            <a href="javascript:void(0)" class="filterby stopPop">Filter by</a>
          </div>
          <div class="accessory-result">
            <!-- filter -->
            <div class="filter stopPop" id="filter">
              <dl class="filter-price">
                <dt>Price:</dt>
                <dd><a href="javascript:void(0)" @click="setPriceFilter('All')">All</a></dd>
                <dd>
                  <a href="javascript:void(0)" @click="setPriceFilter('0')">0 - 100</a>
                </dd>
                <dd>
                  <a href="javascript:void(0)" @click="setPriceFilter('1')">100 - 500</a>
                </dd>
                <dd>
                  <a href="javascript:void(0)" @click="setPriceFilter('2')">500 - 1000</a>
                </dd>
                <dd>
                  <a href="javascript:void(0)" @click="setPriceFilter('3')">1000 - 2000</a>
                </dd>
              </dl>
            </div>

            <!-- search result accessories list -->
            <div class="accessory-list-wrap">
              <div class="accessory-list col-4">
                <ul>
                  <li v-for="item in goodsList">
                    <div class="pic">
                      <a href="#"><img :src="'../../static/'+item.productImage" alt=""></a>
                    </div>
                    <div class="main">
                      <div class="name">{{item.productName}}</div>
                      <div class="price">{{item.salePrice}}</div>
                      <div class="btn-area">
                        <a href="javascript:;" class="btn btn--m" @click="addCartFun(item.productId)">加入购物车</a>
                      </div>
                    </div>
                  </li>
                </ul>
                <div style="clear: both"></div>
                <div class="loadMore" v-infinite-scroll="loadMore" infinite-scroll-disabled="busy" infinite-scroll-distance="10">
                  <img src="../assets/loading-spinning-bubbles.svg" alt="" v-show="loading">
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <nav-footer></nav-footer>
    </div>
</template>

<script>
import NavHeader from  '@/components/Header'
import NavFooter from  '@/components/Footer'
import NavBread from  '@/components/NavBread'
export default{
  data () {
    return {
      goodsList:[],
      sortFlage:true,
      page:0,
      pagesize:8,
      busy: false,
      priceChecked:'all',
      loading:false
    }
  },
  created() {
    this.__init(false)
  },
  components: {
    NavHeader,
    NavFooter,
    NavBread
  },
  methods:{
    __init(flage) {
      this.loading = false;
      this.$http.post('/api/goods',{
        sort:this.sortFlage?1:-1,
        page:this.page,
        pagesize:this.pagesize,
        priceLevel:this.priceChecked
      }).then(res=>{
        this.loading = true;
        if(res.status == '0'){
          if(flage){
            this.goodsList = this.goodsList.concat(res.result.list);
            if(res.result.count == 0){
              this.busy = true;
              this.loading = false;
            }else {
              this.busy = false;
            }
          }else {
            this.loading = false;
            this.goodsList = res.result.list;
          }

        }else {
          this.loading = false;
          this.goodsList = [];
        }
        console.log(res);
      }).catch(err=>{
        this.loading = false;
        this.busy = true;
      })
    },
    //价格排序
    sortGoods(){
      this.sortFlage = ! this.sortFlage;
      this.page = 0;
      this.__init(false);
    },
    //价格区间筛查
    setPriceFilter(index){
      this.priceChecked = index;
      this.page = 0;
      this.__init();
    },
    loadMore(){
      this.busy = true;
      setTimeout(()=>{
        this.page++;
        this.__init(true);
        this.busy = false;
      },500)
    },
    addCartFun(productId){

      // this.$http.post('/api/goods/addcart',{
      //   userId:100000077,
      //   productId:productId
      // }).then(res=>{
      //   console.log(res);
      // }).catch(err=>{
      //   console.log(res);
      // })

      this.$http.post('/api/users/userinfor',{
        userId:100000077,
        productId:productId
      }).then(res=>{
        console.log(res);
      }).catch(err=>{
        console.log(res);
      })
    }
  },
}
</script>
<style scoped>
  .loadMore{
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
</style>
